package com.alex.designPattern.composite;

import java.util.ArrayList;
import java.util.List;

/**
 * 树节点（非叶子节点），使用组合模式
 * @author alex
 *
 */
public class Tree extends Node {
	
	private List<Node> children = new ArrayList<Node>();
	
	public void addChild(Node node) {
		children.add(node);
	}
	
	public void removeChild(Node node) {
		children.remove(node);
	}
	
	public List<Node> getChildren() {
		return children;
	}
	
	@Override
	public void show() {
		showTree(0, this);
	}
	
	private void showTree(int level,Node node) {
		StringBuffer space = new StringBuffer();
		for(int i = level; i > 0; --i) {
			space.append("-");
		}
		System.out.println(space.toString().concat(node.getText()));
		if(node instanceof Tree) {
			Tree tree = (Tree)node;
			for(Node child : tree.getChildren()) {
				showTree(level+1, child);
			}
		}
	}
}
